package com.seeyon.apps.kkARPEvent.event;

import com.seeyon.apps.kkARPEvent.util.FileUploadUtil;
import com.seeyon.apps.kkARPEvent.util.FormToPdf;
import com.seeyon.apps.kkARPEvent.util.KKTokensUtil;
import com.seeyon.ctp.common.affair.manager.AffairManager;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.modules.engin.base.formData.CAP4FormDataDAO;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.po.affair.CtpAffair;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.workflow.event.AbstractWorkflowEvent;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import java.io.File;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: WangL
 * @Date: 2025/3/11 14:03
 * @Description: $:
 */

@Component("KKBusinessProcessIntegrationEvent")
public class KKBusinessProcessIntegrationEvent extends AbstractWorkflowEvent {
    private static final Log log = LogFactory.getLog(KKBusinessProcessIntegrationEvent.class);
    @Inject
    private CAP4FormManager cap4FormManager;
    private static OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
    private static final AffairManager affairManager = (AffairManager) AppContext.getBean("affairManager");
    private CAP4FormDataDAO cap4FormDataDAO;
    @Inject
    private FormApi4Cap4 formApi4Cap4;

    @Override
    public String getId() {
        return "KKBusinessProcessIntegrationEvent";
    }

    @Override
    public String getLabel() {
        return "服务器托管申请表";
    }

    @Override
    public ApplicationCategoryEnum getAppName() {
        return ApplicationCategoryEnum.form;
    }

    /**
     * 流程结束
     *
     * @param data
     * @return
     * @throws BusinessException
     */
    @Override
    public void onProcessFinished(WorkflowEventData data) {
        try {
            long summaryId = data.getSummaryId();
            CtpAffair ctpAffair = affairManager.get(data.getAffairId());
            String toPdf = FormToPdf.cap4FormToPdf(ctpAffair,summaryId);
            log.info(toPdf);// 表单转换PDF
            String accessToken = KKTokensUtil.getAccessToken();
            //String accessToken = "KKTokensUtil.getAccessToken()";
            log.info("客开日志-----------【onProcessFinished】获取到token:"+accessToken);
            if (accessToken != null && !accessToken.isEmpty()) {
                Map<String, Object> map = FileUploadUtil.uploadFile(accessToken, new File(toPdf));
                log.info("客开日志-----------【onProcessFinished】上传文件返回结果:"+map);
            }else {
                throw new BusinessException("客开日志-----------【onProcessFinished】获取token失败");
            }
        } catch (Exception e) {
            log.info("异常"+e.getMessage(),e);
        }
    }
}
